layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 会员信息表管理
     */
    var Member = {
        tableId: "memberTable"
    };

    /**
     * 初始化表格的列
     */
    Member.initColumn = function () {
        return [[
            {type: 'checkbox'},
            // {field: 'memberId',align: 'center',title: '编号', width: 60},
            {
                field: 'avatar', align: 'center', sort: true, title: '头像', templet: function (d) {
                    return "<img src=" + d.avatar + " style='width:42px;height:42px;' />";
                }
            },
            {field: 'name', align: 'center', sort: true, title: '用户名称'},
            {field: 'phone', align: 'center', sort: true, title: '手机号码'},
            // {field: 'sexName',align: 'center',hide: true, sort: true, title: '性别'},
            // {field: 'memberRoleName',align: 'center', sort: true, title: '会员角色'},
            {field: 'company',align: 'center', sort: true, title: '绑定公司'},
            {field: 'postName',align: 'center', sort: true, title: '担任职位'},
            {
                field: 'status', align: "center", sort: true, event: "vali_member", title: '账号状态', templet: function (d) {
                    var flag_box = "<span class=\"layui-btn layui-btn-xs layui-btn-normal \">正常</span>";
                    if (d.status == "ENABLE") {
                        flag_box = "<span class='layui-btn layui-btn-xs layui-btn-normal' >正常</span>";
                    } else if (d.status == "DISABLED") {
                        flag_box = "<span class=\"layui-btn layui-btn-xs layui-btn-disabled \">禁用</span>";
                    }
                    return flag_box;
                }
            },
            {
                field: 'createTime', align: 'center', sort: true, title: '注册时间', templet: function (d) {
                    return "<div>" + layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:ss') + "</div>";
                }
            },
            {field: 'loginTime', align: 'center', hide: true, sort: true, title: '登录时间'},
            // {align: 'center', minWidth: 240, toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Member.search = function () {
        var queryData = {};
        queryData['keyword'] = $("#condition").val();
        table.reload(Member.tableId, {
            where: queryData, page: {curr: 1}
        });
    };


    /**
     * 弹出添加对话框
     */
    Member.openAddDlg = function () {
        func.open({
            title: '添加会员信息表',
            content: Feng.ctxPath + '/member/add',
            tableId: Member.tableId
        });
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Member.openEditDlg = function (data) {
        func.open({
            title: '修改会员信息表',
            content: Feng.ctxPath + '/member/edit?memberId=' + data.memberId,
            tableId: Member.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    Member.exportExcel = function () {
        var checkRows = table.checkStatus(Member.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Member.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/member/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Member.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("memberId", data.memberId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Member.tableId,
        url: Feng.ctxPath + '/member/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Member.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Member.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Member.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Member.exportExcel();
    });


    // 工具条点击事件
    table.on('tool(' + Member.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Member.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Member.onDeleteItem(data);
        } else if (layEvent === 'vali_member' && data.status === 'VALIDATE') {
            Member.onValidate(data);
        }
    });

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Member.onValidate = function (data) {
        $("#phone").val(data.phone);
        $("#name").val(data.name);
        $("#idNumber").val(data.idNumber);
        var m_role = "普通会员";
        if (data.memberRole == 1) {
            m_role = "学生";
        } else if (data.memberRole == 2) {
            m_role = "老师";
        }

        $("#memberRole").val(m_role);
        $("#sex").val(data.sex);
        layer.open({
            type: 1,
            title: "会员认证信息",
            content: $('#div_member_validate'),
            btn: ['确认审核', '取消'],
            yes: function (index, layero) {

                layer.confirm('确认审核通过？', {icon: 3, title: '提示'}, function (index) {
                    var param = {"memberId": data.memberId};
                    $.post(Feng.ctxPath + "/member/veriDateMember", param,
                        function (data) {
                            console.log(data);
                            if (data.success) {
                                layer.alert("处理成功!");
                            } else {
                                layer.alert("系统错误：" + data.error);
                            }
                        });

                    layer.closeAll();
                    Member.search();
                });
            }
        });
    };


    // 修改user状态
    form.on('switch(status)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;
        Member.changeStatus(id, checked);
    });

    /**
     * 修改启用状态
     */
    Member.changeStatus = function (id, checked) {
        var ajax = new $ax(Feng.ctxPath + "/member/changeStatus", function (data) {
            Feng.success("修改成功!");
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message);
            table.reload(Member.tableId);
        });
        ajax.set("id", id);
        ajax.set("status", checked);
        ajax.start();
    };
});
